package day_03;


import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.HashSet;
import java.util.Set;

public class ServerChat extends JFrame {
    public static void main(String[] args) {
        new  ServerChat();
    }
    private JTextArea jta;//文本area
    private JScrollPane jsp;//滚动条
    private JPanel jp;//面板
    private JTextField jtf;//文本框
    private JButton jb;//按钮
    private Socket socket;
    public static Set<Socket> allSocket;//大厅用户

    public JTextArea getJta() {
        return jta;
    }

    public void setJta(JTextArea jta) {
        this.jta = jta;
    }

    public ServerChat(){
        //构建我们的聊天窗口
        jta = new JTextArea();
        jta.setEditable(false);
        jsp = new JScrollPane(jta);
        jp = new JPanel();
        jtf = new JTextField(20);
        jb = new JButton("发送");
        jp.add(jtf);
        jp.add(jb);

        //布局策略
        this.add(jsp, BorderLayout.CENTER);
        this.add(jp, BorderLayout.SOUTH);

        this.setTitle("QQ服务端");
        this.setSize(500,500);
        this.setLocation(100,0);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setVisible(true);

        jb.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {

                System.out.println("按钮被点击");
                //获得文本数据
                sendAdminMsg();
            }
        });
        //监听回车事件
        jtf.addKeyListener(new KeyListener() {
            @Override
            public void keyTyped(KeyEvent e) {

            }
            @Override
            public void keyPressed(KeyEvent e) {
//                System.out.println(e);
                if(e.getKeyCode() == KeyEvent.VK_ENTER){
                    //发送
                    sendAdminMsg();
                }
            }

            @Override
            public void keyReleased(KeyEvent e) {

            }
        });

        //servet start

        ServerSocket server = null ;
        try {
            //创建ServerSocket对象，指定端口是
            server = new ServerSocket(8080);
            System.out.println("服务启动成功");
        } catch (Exception e) {
            System.out.println("服务器启动出错");
        }

        try {
            allSocket = new HashSet<>();
            //调用ServerSocket的accept方法，可以接受客户端的请求
            ServerThread st;
            while(true){
                socket = server.accept();//客户通道
                allSocket.add(socket);
                System.out.println("socket上线成功："+socket);
                //启动服务线程
                st = new ServerThread(socket,this);
                st.start();//服务端接客户端数据
            }
        } catch (Exception e) {
            e.printStackTrace();
        }


        //end start
    }
    //管理员消息
    public void sendAdminMsg(){
        String text = "admin:" + jtf.getText();
        ServerThread.sendMegToAllClient(text);
        jta.append(text + System.lineSeparator());
    }
}